/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import VO.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author oana.leva
 */
public class UserAction extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
        try
        {
        String name=request.getParameter("Name");
        String pwd=request.getParameter("Pwd");

        String emailAddress = request.getParameter("Email");

        //The request comes from the SignUp.jsp page if the text field exists
        if (emailAddress == null)
            Login(name, pwd, out);
        else
            CreateUser(new User(name, pwd), out);
        } finally { 
            out.close();
        }
}

   Vector<User> usersList = null;


   private boolean Login(String user, String pwd, PrintWriter out)
   {
    if (usersList == null) usersList = new Vector<User>();

    if ((UsersListContains(user)&& (PasswordCorrect(user, pwd))))
    {
        out.println("User logat");
        return true;
    }

    
    out.println("---> Unable to logon: Information not found (Please make sure that your user name and password are correct)");
    return false;
}

private void CreateUser(User newUser, PrintWriter out)
{
    if (usersList == null) usersList = new Vector<User>();

    if (UsersListContains(newUser.getName()))
        out.println("User already exists");
    else
    {
        usersList.add(newUser);
        out.println("User "+newUser.getName()+" created");
    }
}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private boolean PasswordCorrect(String user, String pwd) {
        for (User u:usersList)
            if ((u.getName().equals(user))&& (u.getPwd().equals(pwd)))
                return true;
        return false;
    }


   private boolean UsersListContains(String user)
   {
        for(User u:usersList)
            if (u.getName().equals(user))
		return true;
        return false;
   }

}
